package org.alex.samples.service;

import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IExamSubjectDAO;
import org.alex.samples.dao.IExamTestDAO;
import org.alex.samples.dao.IExamTestTypeDAO;
//import org.alex.samples.dao.IExamTestTypeDAO;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamTest;
import org.alex.samples.model.ExamTestType;
import org.alex.samples.model.ExamTestTypeVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.SubjectAndTypes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ManageTestService implements IManageTestService {

	@Autowired
	private IExamTestDAO examTestDAO;
	
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	
	
	
//查询试题	
	@Override
	public ExamTestVO getTestById(Integer testId) {
		ExamTest entity=examTestDAO.get(testId);
	//	System.out.println(entity.getContent());
		if(entity !=null){
			ExamTestVO vo =new ExamTestVO();
			BeanUtils.copyProperties(entity, vo);
			
			// get subject
			ExamSubject subject=examSubjectDAO.getExamSubjectById(entity.getSubId());
			if(subject!=null){
				vo.setSubname(subject.getName());
			}
			
			//convert ExamTestType to ExamTestTypeVO
			ExamTestTypeVO typeVO=new ExamTestTypeVO();
			BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
			vo.setExamTestType(typeVO);	
			
			return vo;			
		}
		return null;
	}

	
//查询全部
	@Override
	public List<ExamTestVO> loadAllTest() {
		List<ExamTest> entities=examTestDAO.loadAll();
		if(entities!=null){
			ExamTestVO vo=null;
			List<ExamTestVO> resultList=new ArrayList<ExamTestVO>();
			for(ExamTest entity:entities){
				vo=new ExamTestVO();
				BeanUtils.copyProperties(entity, vo);
				
				// get subject
				ExamSubject subject=examSubjectDAO.getExamSubjectById(entity.getSubId());
				if(subject!=null){
					vo.setSubname(subject.getName());
				}
				
				//convert ExamTestType to ExamTestTypeVO
				ExamTestTypeVO typeVO=new ExamTestTypeVO();
				BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
				vo.setExamTestType(typeVO);	
				
				resultList.add(vo);   //***
			}
			return  resultList ;
		}
				
		return null;
	}


	
//新增考题信息
	@Override
	public void addTest(ExamTestVO examTestVO) {
		ExamTest examTest=new ExamTest();
		BeanUtils.copyProperties(examTestVO, examTest);
		ExamTestType examTestType=new ExamTestType();
		examTestType.setId(examTestVO.getExamTestType().getId());
		examTest.setExamTestType(examTestType);
		examTest.setSubId(examTestVO.getSubId());
		examTestDAO.addExamTest(examTest);
		
	}


//显示类型和科目
	@Override
	public SubjectAndTypes getSubjectAndTypes() {
		SubjectAndTypes subjectAndTypes=new SubjectAndTypes();
		subjectAndTypes.setExamSubject(examSubjectDAO.findAllExamSubject());
		List<ExamTestTypeVO> examTestTypeVOs=new ArrayList<ExamTestTypeVO>();
		List<ExamTestType> examTestTypes= examTestTypeDAO.findAllType();
		if(examTestTypes!=null){
			for(int i=0;i<examTestTypes.size();i++){
				ExamTestTypeVO examTestTypeVO=new ExamTestTypeVO();
				BeanUtils.copyProperties(examTestTypes.get(i), examTestTypeVO);
				examTestTypeVOs.add(examTestTypeVO);
			}
			subjectAndTypes.setExamTestTypevo(examTestTypeVOs);
		}
		return subjectAndTypes;
		
	}

//删除考题
	@Override
	public void deleteTest(Integer testId) {
		examTestDAO.deleteTest(testId);
		
	}

//修改考题
	@Override
	public void editTest(ExamTestVO examTestVO) {
		ExamTest examTest=new ExamTest();
		BeanUtils.copyProperties(examTestVO, examTest);
		ExamTestType examTestType=new ExamTestType();
		examTestType.setId(examTestVO.getExamTestType().getId());
		examTest.setExamTestType(examTestType);
		examTest.setSubId(examTestVO.getSubId());
		examTestDAO.editExamTest(examTest);
		
	}



	
}
